options compress=YES;
libname manasi ''; 

* CALCULATE SSI RECEIPT FOR PARENTS OF SSI KIDS;

data manasi.par_ssr_prep1;

	set manasi.manasi_ssr(keep=hun pan toa mft rcd_est_jd birth_jd sex elg_rd appl_jd
			dib_dig dib_dig2 fedamt_109-fedamt_468 fedpmt_109-fedpmt_468 
			statpmt_109-statpmt_468);


	* RENAME THE PANEL VARIABLES ;

	%macro array2d_no(var);
	array &var.(1983:2012, 1:12) 	&var._109-&var._468;
  	%mend array2d_no;

	%macro array2d_yrmo(var);
	array &var.(1983:2012, 1:12) 	&var._1983_1-&var._1983_12
									&var._1984_1-&var._1984_12
									&var._1985_1-&var._1985_12
									&var._1986_1-&var._1986_12
									&var._1987_1-&var._1987_12
									&var._1988_1-&var._1988_12
									&var._1989_1-&var._1989_12
									&var._1990_1-&var._1990_12
									&var._1991_1-&var._1991_12
									&var._1992_1-&var._1992_12
									&var._1993_1-&var._1993_12
									&var._1994_1-&var._1994_12
									&var._1995_1-&var._1995_12
									&var._1996_1-&var._1996_12
									&var._1997_1-&var._1997_12
									&var._1998_1-&var._1998_12
									&var._1999_1-&var._1999_12
									&var._2000_1-&var._2000_12
									&var._2001_1-&var._2001_12
									&var._2002_1-&var._2002_12
									&var._2003_1-&var._2003_12
									&var._2004_1-&var._2004_12
									&var._2005_1-&var._2005_12
									&var._2006_1-&var._2006_12
									&var._2007_1-&var._2007_12
									&var._2008_1-&var._2008_12
									&var._2009_1-&var._2009_12
									&var._2010_1-&var._2010_12
									&var._2011_1-&var._2011_12
									&var._2012_1-&var._2012_12;
  	%mend array2d_yrmo;

	%array2d_no(fedamt); %array2d_no(fedpmt); %array2d_no(statpmt);
	%array2d_yrmo(fedamount); %array2d_yrmo(fedpay); %array2d_yrmo(statpay);

	do i = 1983 to 2012;
		do j = 1 to 12;
			fedamount(i,j)=fedamt(i,j);
			fedpay(i,j)=fedpmt(i,j);
			statpay(i,j)=statpmt(i,j);
		end;
	end;

	drop fedamt_:;
	drop fedpmt_:;
	drop statpmt_:;

	* CONVERT THE DATE VARIABLES TO CHARACTER FORM;
	rcd_est_date=put(rcd_est_jd, mmddyy6.); 
	birth_date=put(birth_jd, mmddyy6.);  
	elg_date=put(elg_rd, mmddyy6.); 
	appl_date=put(appl_jd, mmddyy6.);

 run;

data manasi.par_ssr_prep2;
	set manasi.par_ssr_prep1;

* CREATE MACROS FOR ARRAY AND DROP ;
	%macro array1d(var);		
	array &var.1(1:408) 
						&var._1980_1-&var._1980_12
						&var._1981_1-&var._1981_12
						&var._1982_1-&var._1982_12
						&var._1983_1-&var._1983_12
						&var._1984_1-&var._1984_12
						&var._1985_1-&var._1985_12
						&var._1986_1-&var._1986_12
						&var._1987_1-&var._1987_12
						&var._1988_1-&var._1988_12
						&var._1989_1-&var._1989_12
						&var._1990_1-&var._1990_12
						&var._1991_1-&var._1991_12
						&var._1992_1-&var._1992_12
						&var._1993_1-&var._1993_12
						&var._1994_1-&var._1994_12
						&var._1995_1-&var._1995_12
						&var._1996_1-&var._1996_12
						&var._1997_1-&var._1997_12
						&var._1998_1-&var._1998_12
						&var._1999_1-&var._1999_12
						&var._2000_1-&var._2000_12
						&var._2001_1-&var._2001_12
						&var._2002_1-&var._2002_12
						&var._2003_1-&var._2003_12
						&var._2004_1-&var._2004_12
						&var._2005_1-&var._2005_12
						&var._2006_1-&var._2006_12
						&var._2007_1-&var._2007_12
						&var._2008_1-&var._2008_12
						&var._2009_1-&var._2009_12
						&var._2010_1-&var._2010_12
						&var._2011_1-&var._2011_12
						&var._2012_1-&var._2012_12 
						&var._2013_1-&var._2013_12 
						;
	%mend array1d;

	%macro array2d(var);
	array &var.(1980:2013, 1:12) 	
									&var._1980_1-&var._1980_12
									&var._1981_1-&var._1981_12
									&var._1982_1-&var._1982_12
									&var._1983_1-&var._1983_12
									&var._1984_1-&var._1984_12
									&var._1985_1-&var._1985_12
									&var._1986_1-&var._1986_12
									&var._1987_1-&var._1987_12
									&var._1988_1-&var._1988_12
									&var._1989_1-&var._1989_12
									&var._1990_1-&var._1990_12
									&var._1991_1-&var._1991_12
									&var._1992_1-&var._1992_12
									&var._1993_1-&var._1993_12
									&var._1994_1-&var._1994_12
									&var._1995_1-&var._1995_12
									&var._1996_1-&var._1996_12
									&var._1997_1-&var._1997_12
									&var._1998_1-&var._1998_12
									&var._1999_1-&var._1999_12
									&var._2000_1-&var._2000_12
									&var._2001_1-&var._2001_12
									&var._2002_1-&var._2002_12
									&var._2003_1-&var._2003_12
									&var._2004_1-&var._2004_12
									&var._2005_1-&var._2005_12
									&var._2006_1-&var._2006_12
									&var._2007_1-&var._2007_12
									&var._2008_1-&var._2008_12
									&var._2009_1-&var._2009_12
									&var._2010_1-&var._2010_12
									&var._2011_1-&var._2011_12
									&var._2012_1-&var._2012_12
									&var._2013_1-&var._2013_12
									;
  	%mend array2d;


	%macro drop(var);
	drop 	
			&var._1980_1-&var._1980_12
			&var._1981_1-&var._1981_12
			&var._1982_1-&var._1982_12
			&var._1983_1-&var._1983_12
			&var._1984_1-&var._1984_12
			&var._1985_1-&var._1985_12
			&var._1986_1-&var._1986_12
			&var._1987_1-&var._1987_12
			&var._1988_1-&var._1988_12
			&var._1989_1-&var._1989_12
			&var._1990_1-&var._1990_12
			&var._1991_1-&var._1991_12
			&var._1992_1-&var._1992_12
			&var._1993_1-&var._1993_12
			&var._1994_1-&var._1994_12
			&var._1995_1-&var._1995_12
			&var._1996_1-&var._1996_12
			&var._1997_1-&var._1997_12
			&var._1998_1-&var._1998_12
			&var._1999_1-&var._1999_12
			&var._2000_1-&var._2000_12
			&var._2001_1-&var._2001_12
			&var._2002_1-&var._2002_12
			&var._2003_1-&var._2003_12
			&var._2004_1-&var._2004_12
			&var._2005_1-&var._2005_12
			&var._2006_1-&var._2006_12
			&var._2007_1-&var._2007_12
			&var._2008_1-&var._2008_12
			&var._2009_1-&var._2009_12
			&var._2010_1-&var._2010_12
			&var._2011_1-&var._2011_12 
			&var._2012_1-&var._2012_12 
			&var._2013_1-&var._2013_12 
			;
	%mend drop;

	
* CREATE ANNUAL DI PAYMENT VARIABLES, INFLATED ;
	
	%array2d(fedamount); %array2d(fedpay); %array2d(statpay);

	* Annual payment amounts ;
	array parfedamt(1980:2013) n_parfedamt_1980-n_parfedamt_2013;
	array parfedpay(1980:2013) n_parfedpay_1980-n_parfedpay_2013;
	array parstatpay(1980:2013) n_parstatpay_1980-n_parstatpay_2013;


	do i = 1980 to 2013;
		parfedamt(i) = SUM(fedamount(i,1), fedamount(i,2), fedamount(i,3), fedamount(i,4),
						 fedamount(i,5), fedamount(i,6), fedamount(i,7), fedamount(i,8),
						 fedamount(i,9), fedamount(i,10), fedamount(i,11), fedamount(i,12));
		parfedpay(i) = SUM(fedpay(i,1), fedpay(i,2), fedpay(i,3), fedpay(i,4),
				 		 fedpay(i,5), fedpay(i,6), fedpay(i,7), fedpay(i,8),
				 		 fedpay(i,9), fedpay(i,10), fedpay(i,11), fedpay(i,12));
		parstatpay(i) = SUM(statpay(i,1), statpay(i,2), statpay(i,3), statpay(i,4),
				 		 statpay(i,5), statpay(i,6), statpay(i,7), statpay(i,8),
				 		 statpay(i,9), statpay(i,10), statpay(i,11), statpay(i,12));
	end;

	* Indicator for SSI payment in given year;
	array paronssi(1980:2013) n_paronssi_1980-n_paronssi_2013;
	do i = 1980 to 2013;
		if parfedamt(i)>0 then paronssi(i)=1;
			else paronssi(i)=0;
	end;

	* Now inflate each year's payments to 2012 dollars;
	n_cpi_1980=2.79;
	n_cpi_1981=2.53;
	n_cpi_1982=2.38;
	n_cpi_1983=2.30;
	n_cpi_1984=2.21;
	n_cpi_1985=2.13;
	n_cpi_1986=2.09;
	n_cpi_1987=2.03;
	n_cpi_1988=1.94;
	n_cpi_1989=1.86;
	n_cpi_1990=1.76;
	n_cpi_1991=1.69;
	n_cpi_1992=1.64;
	n_cpi_1993=1.59;
	n_cpi_1994=1.55;
	n_cpi_1995=1.51;
	n_cpi_1996=1.47;
	n_cpi_1997=1.43;
	n_cpi_1998=1.41;
	n_cpi_1999=1.38;
	n_cpi_2000=1.34;
	n_cpi_2001=1.3;
	n_cpi_2002=1.28;
	n_cpi_2003=1.25;
	n_cpi_2004=1.22;
	n_cpi_2005=1.18;
	n_cpi_2006=1.14;
	n_cpi_2007=1.11;
	n_cpi_2008=1.07;
	n_cpi_2009=1.07;
	n_cpi_2010=1.06;
	n_cpi_2011=1.02;
	n_cpi_2012=1;
	n_cpi_2013=.99;

	array cpi(1980:2013) n_cpi_1980-n_cpi_2013;
	do i = 1980 to 2013;
		parfedamt(i) = parfedamt(i)*cpi(i);
		parfedpay(i) = parfedpay(i)*cpi(i);
		parstatpay(i) = parstatpay(i)*cpi(i);
	end;

	* Calculate combined federal and state pay ;
	array parcombpay(1980:2013) n_parcombpay_1980-n_parcombpay_2013;
	do i = 1980 to 2013;
		parcombpay(i) = parfedpay(i) + parstatpay(i);
	end;
	
	%drop(fedamount);
	%drop(fedpay);
	%drop(statpay);
	drop n_cpi_:;

run;
